package com.yuanluludev.lib_aop

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.yuanluludev.lib_utils.util.LogUtils
import com.yuanluludev.permission.RxPermissionUtils
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Pointcut

/**   yuanLuLu
 *    权限申请切面
 */
@Suppress("unused")
@Aspect
class PermissionsAspect {

    /**
     * 方法切入点
     */
    @Pointcut("execution(@com.yuanluludev.lib_aop.Permissions * *(..))")
    fun method() {
    }

    /**
     * 在连接点进行方法替换
     */
    @Around("method() && @annotation(permissions)")
    fun aroundJoinPoint(
        joinPoint: ProceedingJoinPoint,
        permissions: Permissions
    ) {

        var activity: FragmentActivity? = null
        var fragment: Fragment? = null

        // 方法参数值集合
        val parameterValues: Array<Any?> = joinPoint.args
        for (arg: Any? in parameterValues) {
            if (arg is FragmentActivity) {
                activity = arg
                continue
            }
            if (arg is Fragment) {
                fragment = arg
                continue
            }
        }
        if (activity != null) {
            if (activity.isFinishing || activity.isDestroyed) {
                LogUtils.d(
                    "AppLog",
                    "The activity has been destroyed and permission requests cannot be made"
                )
                return
            }
        }

        if (fragment != null) {
            if (fragment.activity == null) {
                LogUtils.d("AppLog", "The activity has been destroyed and permission requests cannot be made")
                return
            }
        }

        requestPermissions(
            joinPoint,
            activity,
            fragment,
            permissions.value,
            permissions.isShowGuideDialog
        )
    }

    /**
     * 去申请权限
     */
    private fun requestPermissions(
        joinPoint: ProceedingJoinPoint,
        activity: FragmentActivity?,
        fragment: Fragment?,
        permissions: Array<out String>,
        isShowGuideDialog: Boolean
    ) {
        if (activity != null) {
            RxPermissionUtils.requestPermissionInActivity(
                activity,
                *permissions,
                isShowGuideDialog = isShowGuideDialog,
                onGranted = {
                    try {
                        // 获得权限，执行原方法
                        joinPoint.proceed()
                    } catch (e: Throwable) {
                        LogUtils.d("AppLog", "message = ${e.message} \n localizedMessage = ${e.localizedMessage}  cause = ${e.cause}")
                    }
                })
        }

        if(fragment!=null){
            RxPermissionUtils.requestPermissionInFragment(
                fragment,
                *permissions,
                isShowGuideDialog = isShowGuideDialog,
                onGranted = {
                    try {
                        // 获得权限，执行原方法
                        joinPoint.proceed()
                    } catch (e: Throwable) {
                        LogUtils.d("AppLog", "message = ${e.message} \n localizedMessage = ${e.localizedMessage}  cause = ${e.cause}")
                    }
                })
        }

    }

}